package com.ruoyi.merchant.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.merchant.domain.dto.FlowQueryDTO;
import com.ruoyi.merchant.domain.dto.WithdrawDTO;
import com.ruoyi.merchant.domain.dto.WithdrawQueryDTO;
import com.ruoyi.merchant.domain.vo.*;
import com.ruoyi.merchant.service.IMerchantFinanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商家财务Controller
 */
//商家财务管理
@RestController
@RequestMapping("/merchant/finance")
public class MerchantFinanceController extends BaseController {
    
    @Autowired
    private IMerchantFinanceService merchantFinanceService;
    
    /**
     * 查询账户余额
     */
    @GetMapping("/balance")
    public AjaxResult getBalance() {
        AccountBalanceVO vo = merchantFinanceService.getAccountBalance();
        return AjaxResult.success("查询成功", vo);
    }
    
    /**
     * 查询流水列表
     */
    @GetMapping("/flow-list")
    public TableDataInfo getFlowList(FlowQueryDTO query) {
        startPage();
        List<FlowListVO> list = merchantFinanceService.getFlowList(query);
        return getDataTable(list);
    }
    
    /**
     * 查询流水详情
     */
    @GetMapping("/flow/{flowId}")
    public AjaxResult getFlowDetail(@PathVariable Long flowId) {
        FlowDetailVO vo = merchantFinanceService.getFlowDetail(flowId);
        return AjaxResult.success("查询成功", vo);
    }
    
    /**
     * 提现申请
     */
    @Log(title = "商家财务", businessType = BusinessType.INSERT)
    @PostMapping("/withdraw")
    public AjaxResult applyWithdraw(@Validated @RequestBody WithdrawDTO withdrawDTO) {
        WithdrawResultVO vo = merchantFinanceService.applyWithdraw(withdrawDTO);
        return AjaxResult.success("提现申请已提交", vo);
    }

    /**
     * 查询提现记录列表
     */
    @GetMapping("/withdraw-list")
    public TableDataInfo getWithdrawList(WithdrawQueryDTO query) {
        startPage();
        List<WithdrawRecordVO> list = merchantFinanceService.getWithdrawList(query);
        return getDataTable(list);
    }

}
